package server

import (
	"edata-hub/pkg/raft"
	"gopkg.in/yaml.v3"
	"io"
	"os"
)

// 配置结构体，对应配置文件application.yml的配置
type Config struct {
	raft.RaftConfig `yaml:"raft"`
	MasterConfig    `yaml:"master"`
	WorkerConfig    `yaml:"worker"`
}

type MasterConfig struct {
	EdhCompPath  string `yaml:"edh-comp-path"`
	DataCompPath string `yaml:"data-comp-path"`
	ClustersPath string `yaml:"clusters-path"`
	RemotePath   string `yaml:"remote-path"`
	ProgressPath string `yaml:"progress-path"`
}

// @Description: 对应application.yml中Worker配置块的配置
type WorkerConfig struct {
	Ip       string `yaml:"ip"`
	RpcPort  string `yaml:"rpc-port"`
	HttpPort string `yaml:"http-port"`
	LogPath  string `yaml:"log-path"`
	MsgLevel int    `yaml:"msg-level"`
	TicTok   int    `yaml:"tiktok"`
	Timeout  int    `yaml:"timeout"`
}

// @Description: 读取配置文件
// @receiver c
// @param path: 配置文件路径
func ReadConfig(path string) (Config, error) {
	var config Config
	file, openFileErr := os.Open(path)
	defer file.Close()
	if openFileErr != nil {
		return config, openFileErr
	}
	data, readFileErr := io.ReadAll(file)
	if readFileErr != nil {
		return config, readFileErr
	}
	unmarshalErr := yaml.Unmarshal(data, &config)
	if unmarshalErr != nil {
		return config, unmarshalErr
	}
	return config, nil
}
